<?php

namespace Apps\AppApi\Method\User;


use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\UserBase;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\SysAreaZipDistrictPhonecode;
use Apps\Common\Models\UserFollow;

class GetHisInfo extends BaseMethod
{
    public function run()
    {
        $to_user_id = $this->getDataItem('user_id');

        if (!(is_numeric($to_user_id) && $to_user_id > 0)) {
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '用户id不能为空.');
        }
        /**
         * 验证用户权限
         */
        /*if(!empty($this->parameters->userToken) && $this->verifyUserAuth() && $this->getUserAuth()->userId == $userid){
            return $this->errorLog(111, '请跳转我的问答.');
        }*/

        $field = [
            'user_cover','user_nickname','user_gender',
            'user_birthday','user_realname','user_district',
            'user_money', 'user_money_virtual', 'bind_phone user_mobile',
            'persional_sign', 'user_expert', 'expert_follow_count'
        ];
        $info = UserBase::query()->columns($field)->where('user_id='.$to_user_id)->execute()->getFirst()->toArray();

        // 获取城市信息
        $SysAreaZipDistrictPhonecode = new SysAreaZipDistrictPhonecode();
        $city = $SysAreaZipDistrictPhonecode->getOne($info['user_district']);
        $info['user_district'] = $city['FULL_NAME'] ?: '';

        // 加入关注数和粉丝数
        $UserFollow = new UserFollow();
        $info['follow_count'] = $UserFollow->getFollowCount($to_user_id, 1);
//        $info['fans_count'] = $UserFollow->getToFollowCount($to_user_id, 1);

        if ($info['user_expert']) {
            $info['fans_count'] = $info['expert_follow_count'];
        } else {
            $info['fans_count'] = $UserFollow->getToFollowCount($to_user_id, 1);
        }

        $info['user_cover'] = $info['user_cover'] ? PicUrl::UserCover($info['user_cover'], $this->getDi()) : '';
        $info['persional_sign'] = $info['persional_sign'] ? unserialize(base64_decode($info['persional_sign'])) : '';

        $this->verifyUserAuth(false);
        if ($user_id = $this->getUserAuth()->userId) {
            $info['is_follow'] = $UserFollow->is_follow($user_id, $to_user_id, 1) ? '1' : '0';
        } else {
            $info['is_follow'] = '0';
        }

        $this->setResult($info);
    }
}